<?php 
## include required files
/*******************************/
require_once '../model/state.php';
/*******************************/
## Create Objects
/*******************************/
$countryObj = new Model_State();
$stateObj = new Model_State();
/*******************************/

if(isset($_POST['editstate_btn']) || $_POST['statename']) {
	
	## apply PHP validation for required filed
	if( trim($_POST['country']) != '' && $_POST['statename'] != '' && trim($_POST['siso_code']) != '') {	
		extract($_POST);
		$getdetailstate = $stateObj->checkStateName($_POST['statename'],$_POST['stateId']);
		if($getdetailstate['state_id']=='')
		{
			$stateeditArray['country_id']   = return_post_value($_POST['country']);
			$stateeditArray['state_name']   = return_post_value($_POST['statename']);		
			$stateeditArray['iso1_code']      = return_post_value($_POST['siso_code']);		
			$stateObj->editStateById($stateeditArray,$_POST['stateId']);
			$_SESSION['msg'] = "<div class='success_msg'><span>State edited successfully</span></div>";
			header('location:'.SITE_URL.'/admin/home.php?q=state&page='.$_GET['page']);
			exit;
		}
		else
		{// php validation else
			$_SESSION['msg'] = '<div class="error_msg"><span>State name already used.</span></div>';
		}
	} else {// php validation else
		$_SESSION['msg'] = '<div class="error_msg"><span>Please fill all required fields</span></div>';
	}
	// main if closed
}


##
$countryArray=$countryObj->getAllCountryWithActiveStatus();
$smarty->assign('countryArray', $countryArray);

if($_GET['id'] != ''&& $_GET['action'] == 'edit') {
	$state_id = base64_decode($_GET['id']);
	$stateDetArray = $stateObj->getStateDetailsById($state_id);	
	$smarty->assign('stateDetArray', $stateDetArray);
}

## Assign success or error msg to smarty variable and unset session variable
if(trim($_SESSION['msg']) != '') {
	$smarty->assign('msg', $_SESSION['msg']);
	unset($_SESSION['msg']);
}

## Set active class variable for left menu
$smarty->assign('activeclass', 'state');
$smarty->assign('mainmenu', '2');
## Unset all the objects created which are on this page
unset($countryObj);
unset($stateObj);



$smarty->display(TEMPLATEDIR_ADMIN. 'state/editstate.tpl');
unset($smarty);
?>